home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3614 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What does the -O option do?
  5. Date: 29 Jan 1996 12:37:26 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ejb66INN111@keats.ugrad.cs.ubc.ca>
  8. References: <4eh2ve$4u2@mark.ucdavis.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4eh2ve$4u2@mark.ucdavis.edu>,
  12. Ricardo E Espinoza-Ibarra <espinoza@cs.ucdavis.edu> wrote:
  13. >Does anyone know what the -O option in gcc C compiler does?  I have tried to compile a program in two different ways: 1) cc -o first first.c, and it worksfine; 2) cc -o first first.c -O, and it runs faster than the other one.  I checked to see if it had changed the program code in anyway, and it hadn't.
  14. >This option stands for "optimizer", but I want to know how the heck it optimizes the program!  Please respond as soon as possible.
  15.  
  16. It optimizes by generating different machine code for the same C code. Machine
  17. code which produces the same computation but using fewer CPU cycles. Sometimes
  18. optimized code is longer than unoptimized, but usually it is shorter.
  19.  
  20. Code can be optimized when it is still in an intermediate representation. In
  21. fact, it has to be optimized, because the initial code that is generated from
  22. the C source has all kinds of redundant instructions.
  23.  
  24. One of the most common things that is done is to prune instructions that
  25. calculate duplicate results, results which are never used and such.
  26. Optimization also involves reducing the strength of operations. For example, a
  27. division of an integer by two can be done by a shift right. 
  28.  
  29. There is also machine-specific optimization, which is typically done by a
  30. "peephole" method. A narrow window of instructions from each basic block is
  31. scanned top to bottom. If the sequence in the "peephole" matches the criteria
  32. of some sort of pattern, it is replaced by an equivalent sequence that is
  33. faster.
  34.  
  35. Also, the way registers are allocated during target code generation is also
  36. important. When a compiler is told to optimize, it should also try to be smart
  37. about how it allocates registers to variables.
  38.  
  39. The GNU compiler has a module called "stupid.c" which performs "stupid"
  40. register allocation, and is only invoked when you _don't_ use the -O flag.
  41.  
  42. There are many clever things that an optimizer can do. Compiler designers are
  43. endlessly creative about it. One optimization, for instance, is known as 'jump
  44. threading'. If the target of one jump instruction is another jump instruction,
  45. the original jump is redirected to go directly to the target. Such code can
  46. arise from nested block statements in the original code. For example, an inner
  47. loop can exit by jumping to the instruction that follows the loop. But that
  48. instruction can be the last statement of an outer loop which jumps back to the
  49. top. Hence the inner jump should just go right for the top.  The initial
  50. translation stages might not catch something like that, since they are driven
  51. by the syntax of the nested block.
  52.  
  53. To see the difference between -O and not -O, try using the -S flag to generate
  54. intermediate assembly. Use GCC, if you can. The default system compiler might
  55. not support the generation of assembly code.
  56.  
  57. -- 
  58.  
  59.